package com.xjrsoft.module.interfaceManager.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.node.ForestNodeMerger;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.utils.TreeNodeUtil;
import com.xjrsoft.module.interfaceManager.dto.AddCategoryDto;
import com.xjrsoft.module.interfaceManager.entity.XjrInterfaceCategory;
import com.xjrsoft.module.interfaceManager.entity.XjrInterfaceInfo;
import com.xjrsoft.module.interfaceManager.service.IXjrInterfaceCategoryService;
import com.xjrsoft.module.interfaceManager.service.IXjrInterfaceInfoService;
import com.xjrsoft.module.interfaceManager.vo.CategoryTreeVo;
import com.xjrsoft.module.interfaceManager.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 接口管理分类管理 前端控制器
 * </p>
 *
 * @author tzx
 * @since 2022-01-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/xjr-interface-category")
@Api(value = "/xjr-interface-category", tags = "接口管理分类")
public class XjrInterfaceCategoryController {

    private IXjrInterfaceCategoryService categoryService;

    private IXjrInterfaceInfoService infoService;


    @PostMapping
    @ApiOperation(value="新增分类详情")
    public Response<Boolean> add(@RequestBody AddCategoryDto dto){
        XjrInterfaceCategory category = BeanUtil.copy(dto, XjrInterfaceCategory.class);
        if (StringUtil.isEmpty(category.getParentId())) {
            category.setParentId(StringPool.ZERO);
        }
        return Response.ok(categoryService.save(category));
    }

    @GetMapping("/{id}")
    @ApiOperation(value="获取分类详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "string")
    public Response<CategoryVo> getById(@PathVariable String id){
        XjrInterfaceCategory category = categoryService.getById(id);
        return Response.ok(BeanUtil.copy(category,CategoryVo.class));
    }

    @GetMapping("/tree")
    @ApiOperation(value="获取分类树结构")
    public Response<List<CategoryTreeVo>> getCategoriesTree(String keyword){
        List<XjrInterfaceCategory> interfaceCategoryList = categoryService.list(Wrappers.<XjrInterfaceCategory>query().lambda()
                .like(!StringUtil.isEmpty(keyword), XjrInterfaceCategory::getName, keyword).orderByAsc(XjrInterfaceCategory::getSortCode));
            List<CategoryTreeVo> interfaceCategoryTreeVoList = BeanUtil.copyList(interfaceCategoryList, CategoryTreeVo.class);
        return Response.ok(ForestNodeMerger.merge(interfaceCategoryTreeVoList));
    }


    @PutMapping("/{id}")
    @ApiOperation(value="修改")
    public Response<Boolean> add(@PathVariable String id, @RequestBody AddCategoryDto dto){
        XjrInterfaceCategory category = BeanUtil.copy(dto, XjrInterfaceCategory.class);
        category.setId(id);
        if (StringUtil.isEmpty(category.getParentId())) {
            category.setParentId(StringPool.ZERO);
        }
        return Response.ok(categoryService.updateById(category));
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value="删除")
    public Response<Boolean> delete(@PathVariable String id){
        List<XjrInterfaceCategory> interfaceCategoryList = categoryService.list();
        List<CategoryTreeVo> interfaceCategoryTreeVoList = BeanUtil.copyList(interfaceCategoryList, CategoryTreeVo.class);
        ForestNodeMerger.merge(interfaceCategoryTreeVoList);
        CategoryTreeVo target = null;
        for (CategoryTreeVo interfaceCategoryTreeVo : interfaceCategoryTreeVoList) {
            if (StringUtil.equals(id, interfaceCategoryTreeVo.getId())) {
                target = interfaceCategoryTreeVo;
            }
        }
        List<CategoryTreeVo> deletedList = TreeNodeUtil.getNodeListOfTree(target);
        List<String> deletedIdList = deletedList.stream().map(entity -> entity.getId()).collect(Collectors.toList());
        boolean flag=false;
       if(categoryService.removeByIds(deletedIdList)){
           //删除分类下的所有接口
            flag = infoService.remove(Wrappers.<XjrInterfaceInfo>query().lambda().in(XjrInterfaceInfo::getCategoryId, deletedIdList));
       }
        return Response.ok(flag);
    }





}
